package com.wyait.manage.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 * 
 * @author zxl
 */
public class DateUtil
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final Date parse(final String dateStr)
    {
        try {
			return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return null;
    }
    public static final String parseDateToStr(final String format, final Date date)
    {
    	return new SimpleDateFormat(format).format(date);
    }
    
    public static final String getDateFormate( final Date date, final String format) {
    	return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }
    /**
	 *********************************************************************************************************
	 * java.lang.String date convert java.util.Date 
	 * @param sDate : yyyy-MM-dd HH:mm:ss
	 * @return : 
	 *********************************************************************************************************
	 */
	public static Date parseDate(String sDate,String...formate) {
		SimpleDateFormat simpleDateFormate = null;
		try {
			if(formate!=null && formate.length>0)
				simpleDateFormate=new SimpleDateFormat(formate[0]);
			else
				simpleDateFormate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
			return simpleDateFormate.parse(sDate);
		} catch (ParseException e) {
			return null;
		}
	}
	public static String format(Date sDate,String...formate) {
		SimpleDateFormat simpleDateFormate = null;
		try {
			if(formate!=null && formate.length>0)
				simpleDateFormate=new SimpleDateFormat(formate[0]);
			else
				simpleDateFormate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			return simpleDateFormate.format(sDate);
		} catch (Exception e) {
			return null;
		}
	}
}
